/*  
	Title: main

	Este fichero contiene la declaración de la clase Main.
	Supune a su vez el hilo principal JavaScript de la página Web.
 */


/*	Constant: WeB_PAGES_SRC
	Dirección de las WebPages
*/
var WEB_PAGES_SRC =
	{
		HOME:			"home.html",
		CONTACTO:		"contacto.html",
		PORTAFOLIO:		"portafolio.html",
		BLOG:			"blog.html"
	};


/*	Variable: main
	Se trata del objeto GLOBAL que contiene la clase Main
*/
var main = false;

SWFAddress.addEventListener(SWFAddressEvent.CHANGE, function(event) 
{ 
	if(!main) 
		main = new Main;

	main.changePath(event);

});


/* 	Class: Main
	La clase Main contiene el hilo principal de la página web.

*/

/* 	Constructor: Main
	Es el constructo de la clase Main
	- Carga las distintas <WebPages>
	- Activa los eventos globales
 */
function Main()
{
	var self = this;
	this.xlayer = false;
	this.div_cont = document.getElementById("rbcontent");

	
	this.web_pages =
		{
			home:			new WebPage(this.div_cont, new Home, WEB_PAGES_SRC.HOME),
			portafolio:		new WebPage(this.div_cont, false, WEB_PAGES_SRC.PORTAFOLIO),
			contacto:		new WebPage(this.div_cont, new Contacto, WEB_PAGES_SRC.CONTACTO),
			blog:			new WebPage(this.div_cont, new Blog, WEB_PAGES_SRC.BLOG)
		};
	this.current_web_page = false;

	for(var wp in this.web_pages)
	{
		this.web_pages[wp].attachEvent("onLoadContentStart", function() { self.loadingStart(); });
		this.web_pages[wp].attachEvent("onLoadContentFinish", function() { self.loadingStop(); });
	}

	this.setEvents();
}

Main.prototype =
{
	setEvents:			function()
	{
		var self = this;

		// Cambio de sección
	},

	loadingStart:		function()
	{
		this.xlayer = new XLayer(document.body, "xlayer", 50);
	},

	loadingStop:		function()
	{
		this.xlayer.stop(5);
	},

	changePath:		function(event)
	{
		if(event.path)
		{
			var page = false;
			if(event.path.match(/home/i))
			{
				page = this.web_pages.home;
			}
			else if(event.path.match(/portafolio/i))
			{
				page = this.web_pages.portafolio;
			}
			if(event.path.match(/contacto/i))
			{
				page = this.web_pages.contacto;
			}
			else if(event.path.match(/blog/i))
			{
				page = this.web_pages.blog;
			}

			if(page && page != this.current_web_page)
			{
				this.current_web_page = page;
				this.current_web_page.loadPage();
			}
			else
			{
				if(!this.current_web_page)
				{
					this.current_web_page = this.web_pages.home;
					this.current_web_page.loadPage();
				}
			}
		}
	}
}


